/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author:  LiuTao
 * Version: 1.0
 * Date:    2024-01-03  18:48
 */
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LibraryManagementSystem extends JFrame {
    private JButton btnSearch, btnBorrow, btnReturn;
    private JTextField txtBookTitle, txtUserId;

    public LibraryManagementSystem() {
        setTitle("Library Management System");
        setSize(400, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 创建界面元素
        btnSearch = new JButton("Search Book"x);
        btnBorrow = new JButton("Borrow Book");
        btnReturn = new JButton("Return Book");
        txtBookTitle = new JTextField(20);
        txtUserId = new JTextField(10);

        // 添加事件监听器
        btnSearch.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 实现图书搜索功能
                searchBook(txtBookTitle.getText());
            }
        });

        btnBorrow.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 实现借书功能
                borrowBook(txtBookTitle.getText(), Integer.parseInt(txtUserId.getText()));
            }
        });

        btnReturn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 实现还书功能
                returnBook(txtBookTitle.getText(), Integer.parseInt(txtUserId.getText()));
            }
        });

        // 布局界面
        JPanel panel = new JPanel();
        panel.add(new JLabel("Book Title:"));
        panel.add(txtBookTitle);
        panel.add(new JLabel("User ID:"));
        panel.add(txtUserId);
        panel.add(btnSearch);
        panel.add(btnBorrow);
        panel.add(btnReturn);

        add(panel);
        setVisible(true);
    }

    private void searchBook(String title) {
        // 实现图书搜索的代码
        // 查询数据库并显示结果
        JOptionPane.showMessageDialog(this, "Searching for book: " + title);
    }

    private void borrowBook(String title, int userId) {
        // 实现借书的代码
        // 更新数据库并显示结果
        JOptionPane.showMessageDialog(this, "Borrowing book: " + title + " for User ID: " + userId);
    }

    private void returnBook(String title, int userId) {
        // 实现还书的代码
        // 更新数据库并显示结果
        JOptionPane.showMessageDialog(this, "Returning book: " + title + " for User ID: " + userId);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new LibraryManagementSystem();
            }
        });
    }
}

